module.exports = function(io) {

    let scoketList = {};
    let users = [];
    io.on('connection', (socket) => {
        console.log("连接好了", socket.id);
        //加入
        socket.on("join", username => {
                socket.name = username;
                // console.log("加入", socket.name)
                scoketList[username] = socket.id;
                let user = { name: username, id: socket.id, tip: false };
                users.push(user)
                    // socket.broadcast.emit("welcome", username) //只显示自己
                io.emit("welcome", username)
                io.emit("myself", username, users, socket.id)
                    // console.log(111, username, users, socket.id)
            })
            //接收消息
        socket.on('chat', res => {
            socket.broadcast.emit("getMsg", res)
                // io.emit("getMsg", res)
        });
        socket.on('otochat', data => {
            //uid重复未解决
            // socket.to(data.tid).emit("otogetMsg", data)
            console.log(data)
                // io.emit("getMsg", res)
        });









        //离开时
        socket.on('disconnect', (reason) => {
            // console.log("断开连接" + socket.id);
            if (scoketList.hasOwnProperty(socket.name)) {
                delete scoketList[socket.name];
                for (let i in users) {
                    if (users[i].name == socket.name) {
                        users.splice(i, 1)
                    }
                }
                socket.broadcast.emit("quit", socket.name, users)
            }
        });
    });


}